package com.guddqs.monkey.shop.utils;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;

/**
 * @author wq
 * @date 2018/11/27
 * @description jd-plus
 */
public class ThreadUtil {

    private static ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("ThreadUtil-pool-%d").build();
    private static ExecutorService multiThreadPool = new ThreadPoolExecutor(2, 50,
            20L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(20), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

    public static void changeSize(Integer corePoolSize, Integer maxPoolSize, Integer queueSize) {
        multiThreadPool = new ThreadPoolExecutor(corePoolSize, maxPoolSize,
                10L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(queueSize), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
    }

    public static void execute(Runnable task) {
        multiThreadPool.execute(task);
    }

    public static void execute(Runnable task, Integer times) {
        for (int i = 0; i < times; i++) {
            multiThreadPool.execute(task);
        }
        multiThreadPool.shutdown();
    }

}
